

package com.nn.smart.bus.controller;

import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.ExcelUtils;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.commons.tools.validator.AssertUtils;
import com.nn.smart.bus.commons.tools.validator.ValidatorUtils;
import com.nn.smart.bus.commons.tools.validator.group.AddGroup;
import com.nn.smart.bus.commons.tools.validator.group.UpdateGroup;
import com.nn.smart.bus.commons.tools.validator.group.DefaultGroup;
import com.nn.smart.bus.dto.NnclNncompanyDTO;
import com.nn.smart.bus.excel.NnclNncompanyExcel;
import com.nn.smart.bus.service.NnclNncompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 公交公司
 *
 * @author nanning nanning@123.com
 * @since v1.0.0 2021-08-25
 */
@RestController
@RequestMapping("nnclnncompany")
public class NnclNncompanyController {

    @Autowired
    private NnclNncompanyService nnclNncompanyService;

    @GetMapping("page")
    public Result<PageData<NnclNncompanyDTO>> page(@RequestParam Map<String, Object> params){
//        PageData<NnclNncompanyDTO> page = nnclNncompanyService.page(params);
        PageData<NnclNncompanyDTO> page = nnclNncompanyService.getPhrasePage(params);
        return new Result<PageData<NnclNncompanyDTO>>().ok(page);
    }

    @GetMapping("{id}")
    public Result<NnclNncompanyDTO> get(@PathVariable("id") String id){
        NnclNncompanyDTO data = nnclNncompanyService.get(id);
        return new Result<NnclNncompanyDTO>().ok(data);
    }

    @PostMapping
    public Result save(@RequestBody NnclNncompanyDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        nnclNncompanyService.save(dto);
        return new Result();
    }

    @PutMapping
    public Result update(@RequestBody NnclNncompanyDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        nnclNncompanyService.update(dto);
        return new Result();
    }

    @DeleteMapping
    public Result delete(@RequestBody String[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        nnclNncompanyService.delete(ids);
        return new Result();
    }

    @GetMapping("export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<NnclNncompanyDTO> list = nnclNncompanyService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, list, NnclNncompanyExcel.class);
    }

}
